package com.zx.disneyholidays.page;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ViewModelProviders;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.StatusBarManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.disneyholidays.R;
import com.zx.disneyholidays.net.IGetDataCallBack;
import com.zx.disneyholidays.page.login.LoginRepository;
import com.zx.disneyholidays.page.login.LoginViewModel;
import com.zx.disneyholidays.utils.StatusBarUtils;

import okhttp3.OkHttpClient;

public class LoginActivity extends AppCompatActivity {

    private EditText etPhone;
    TextView tvLogin;
    LoginViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        StatusBarUtils.setColor(this, Color.WHITE);
        StatusBarUtils.setTextDark(this, true);

        ActivityManager activityManager= ((ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE));
        getLifecycle().addObserver(new LifecycleObserver() {
            @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
            void onResume(){
                Log.d("", "Lifecycle call onResume");
            }
            @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
            void onPause(){
                Log.d("", "Lifecycle call onPause");
            }
        });

        viewModel = ViewModelProviders.of(this).get(LoginViewModel.class);

        etPhone = findViewById(R.id.et_phone);
        tvLogin = findViewById(R.id.tv_login);

        handleEvent();


        viewModel.loginMutableLiveData.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    if (jumpTarget == 0) {
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    } else {
                        startActivity(new Intent(LoginActivity.this, CardEditActivity.class));
                    }
                }
            }
        });
    }

    int jumpTarget = 0;

    private void handleEvent() {

        tvLogin.setOnClickListener(v -> {
            if (etPhone.getText().length() < 11) {
                Toast.makeText(LoginActivity.this, "请确认手机号输入是否有误", Toast.LENGTH_SHORT).show();
                return;
            }

            jumpTarget = 0;
            viewModel.login(etPhone.getText().toString());
        });


        findViewById(R.id.tv_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpTarget = 1;
                viewModel.login(etPhone.getText().toString());
            }
        });
    }


    // 字体大小不跟随系统
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(getConfigurationContext(newBase));
    }

    private static Context getConfigurationContext(Context context) {
        Configuration configuration = context.getResources().getConfiguration();
        configuration.fontScale = 1;
        return context.createConfigurationContext(configuration);
    }

   /* @Override
    public Resources getResources() {
        Resources res=super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics());
        return res;
    }*/
}